#include "RelayMotor.h"

void Relay_Gpio_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOE,ENABLE);

	/*********************************继电器*************************************/
	
	GPIO_InitStructure.GPIO_Pin = RELAY1_PIN|RELAY2_PIN|RELAY3_PIN;  //C
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//下拉
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = RELAY4_PIN;//E
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//下拉
	GPIO_Init(GPIOE, &GPIO_InitStructure);

}

static uint8_t RelayNum = 0,RelayOnOff = 0,RelayDir = 0;
void Set_Relay(uint8_t num,uint8_t onoff,uint8_t dir)
{
	RelayNum = num;
	RelayOnOff = onoff;
	RelayDir = dir;
    Relay_Process(RelayNum,RelayOnOff,RelayDir);
}

void Relay_Ctrl(void)
{
	Relay_Process(RelayNum,RelayOnOff,RelayDir);
}

void Relay_Process(uint8_t num,uint8_t onoff,uint8_t dir)
{
	if(num==_RELAYNONE)
	{
		return;
	}
	if(num==_RELAYONE){
		if(onoff!=0){
			RELAY1_ON();
		}
		else{
			RELAY1_OFF();
		}
	}
	else if(num==_RELAYTWO){
		if(onoff!=0){
			RELAY2_ON();
		}
		else{
			RELAY2_OFF();
		}
	}
	else if(num==_RELAYTHREE){
		if(onoff!=0){
			RELAY3_ON();
		}
		else{
			RELAY3_OFF();
		}
	}
	else if(num==_RELAYFOUR){
		if(onoff!=0){
			RELAY4_ON();
		}
		else{
			RELAY4_OFF();
		}
	}
}


